home *** CD-ROM | disk | FTP | other *** search
/ Aminet 28 / Aminet 28 (1998)(GTI - Schatztruhe)[!][Dec 1998].iso / Aminet / util / libs / MMULib.lha / MMULib / Lib_Sources / mu_tablebuild.asm < prev    next >
Encoding:
Assembly Source File  |  1998-10-04  |  9.6 KB  |  245 lines

  1. ;*************************************************************************
  2. ;** mmu.library                                                         **
  3. ;**                                                                     **
  4. ;** a system library for arbitration and control of the MC68K MMUs      **
  5. ;**                                                                     **
  6. ;** © 1998 THOR-Software, Thomas Richter                                **
  7. ;** No commercial use, reassembly, modification without prior, written  **
  8. ;** permission of the authors.                                          **
  9. ;** Including this library in any commercial software REQUIRES a        **
  10. ;** written permission and the payment of a small fee.                  **
  11. ;**                                                                     **
  12. ;** This is an internal header file, do not depend on anything here.    **
  13. ;** Use the official include files.                                     **
  14. ;** Distributed only for the mmu.library development group for private  **
  15. ;** use.                                                                **
  16. ;**                                                                     **
  17. ;**---------------------------------------------------------------------**
  18. ;** Unified MMU table builder,  Version 0.00                            **
  19. ;**     © 1998  THOR-Software                                           **
  20. ;*************************************************************************
  21.  
  22. ;FOLD Includes
  23.         include inc:macros.asm
  24.         include inc:exec_lib.asm
  25.         include mu_lib.i
  26.         include mu_context.i
  27.         include mu_alerts.i
  28. ;ENDFOLD
  29. ;FOLD External References
  30.         xref LockContext
  31.         xref UnlockContext
  32.         xref MUAlert
  33.         xref FreePointerTable
  34. ;ENDFOLD
  35. ;FOLD Defines
  36. ;ENDFOLD
  37.  
  38.         section main_code,code
  39.  
  40.         machine mc68020
  41.  
  42. ;FOLD BuildMMUTable
  43. ;*************************************************
  44. ;** BuildMMUTable                               **
  45. ;** build a new MMU table or rebuild the MMU    **
  46. ;** table for the context *a0                   **
  47. ;*************************************************
  48.         xdef BuildMMUTable
  49. BuildMMUTable:
  50.  
  51.         illegal
  52.  
  53.         saveregs d2-d5/a3/a5
  54.         move.l a0,a5
  55.         bsr LockContext
  56.  
  57.         moveq #-1,d0                            ;return code is fine
  58.         btstm ctxf_dirty,ctx_Flags(a5)          ;something requires rebuilding it?
  59.         beq .notdirty                           ;if this context is not dirty, nothing needs to be done
  60.         tst.b mulib_MMUType(a6)                 ;A MMU active ?
  61.         beq.s .notdirty                         ;if not, forget it.
  62.  
  63.         move.l ctx_Mapping(a5),a3               ;get the mapping of this context
  64.         do
  65.          tst.l (a3)                             ;end of the list?
  66.          break.s eq                             ;if so, ignore it
  67.          btstm mapf_dirty,map_Flags(a3)         ;does this map node require a rebuild ?
  68.          beq.s .next                            ;if not, get the next
  69.  
  70.          movem.l map_lower(a3),d2-d3            ;read the range of this mapping
  71.          moveq #-1,d4                           ;get highest level that allows mapping of this with an early termination
  72.          moveq #1,d5                            ;size of the "pages" at this level
  73.          addq.l #1,d3                           ;upper and lower bound, upper exclusive
  74.          move.b ctx_LevelABits(a5,d4.w),d0      ;get shift value
  75.          bne.s .shift
  76.  
  77.          do
  78.           addq.l #1,d4                          ;next level
  79.           move.b ctx_LevelABits(a5,d4.w),d0     ;read next level
  80.           bne.s .gotbits
  81.           moveq #4,d4                           ;we're at page level
  82. .gotbits:
  83.           cmp.l d2,d3                           ;is this now equal?
  84.           beq.s .foundlevel                     ;if so, we got the right level
  85. .shift:
  86.           ror.l d0,d5
  87.           lsl.l d0,d2
  88.           lsl.l d0,d3
  89.           cmp.w #4,d4                           ;until we are at page level
  90.          while.s lo
  91.  
  92.          pea AN_BadContext                      ;submit an alert.
  93.          bsr MUAlert                            ;the context contains
  94.          moveq #0,d0                            ;unaligned structures
  95.          bra.s .unlockexit
  96. .foundlevel:                                    ;d4 is now the level of the
  97.                                                 ;MMU table we've to change
  98.          movem.l map_lower(a3),d2-d3            ;get lower/higher address
  99.          addq.l #1,d3
  100.          do
  101.                                                 ;lower in d2
  102.           bsr FindPageDescriptor                ;find the page descriptor that manages this
  103.           beq.s .cleanup
  104.           bsr ModifyPages                       ;setup the pages
  105.           beq.s .cleanup
  106.           add.l d5,d2
  107.           cmp.l d3,d2                           ;all done?
  108.          while.s ne
  109. .next:
  110.          move.l (a3),a3                         ;get the next mode
  111.         loop.s
  112.                                                 ;more to be done here
  113.         moveq #-1,d0
  114.         bra.s .unlockexit
  115. .cleanup:                                       ;more has to happen here
  116.         moveq #0,d0
  117. .unlockexit:
  118. .notdirty:
  119.         move.l a5,a0                            ;Context -> a0
  120.         bsr UnlockContext
  121.  
  122.         tst.l d0
  123.         loadregs
  124.         rts
  125. ;ENDFOLD
  126. ;FOLD FindPageDescriptor
  127. ;*************************************************
  128. ;** FindPageDescriptor                          **
  129. ;** Find the page descriptor for address d2     **
  130. ;** at level d4, return it in d0                **
  131. ;** *a5=Context                                 **
  132. ;** Rebuild, if required, intermediate levels   **
  133. ;** of the translation tree                     **
  134. ;** return the page/pointer descriptor in d0    **
  135. ;** or NULL in case of failure                  **
  136. ;*************************************************
  137. FindPageDescriptor:
  138.         move.l d2,d0
  139.         move.l d4,d1
  140.         jsr mulib_FindPage(a6)                  ;let the MMU specific
  141.                                                 ;routine do the dirty work
  142.         tst.l d0                                ;found something ?
  143.         bne.s .exit                             ;if so, great!
  144.  
  145.         move.l d4,d0
  146.         beq.s .rootlevel
  147.  
  148.         saveregs d4                             ;not found.
  149.                                                 ;if so, find and build the parent level of the tree
  150.         do
  151.          subq.l #1,d4
  152.          tst.b mulib_LevelABits(a6,d4.w)        ;is this level of the tree available ?
  153.         while.s eq                              ;if not, retry
  154.  
  155.                                                 ;here: found a valid (non-root) level
  156.         bsr FindPageDescriptor                  ;find the page descriptor at one level above
  157.         loadregs
  158.         beq.s .exit                             ;if not found, abort
  159.  
  160. .rootlevel:
  161.         move.l d0,a1                            ;page found -> a1 (is null if root)
  162.         move.l d4,d1                            ;this level
  163.         move.l d2,d0                            ;this address
  164.         jsr mulib_InsertLevel(a6)               ;fill in a copy of the tree at this level
  165.         tst.l d0
  166. .exit:
  167.         rts
  168. ;ENDFOLD
  169. ;FOLD ModifyPages
  170. ;*************************************************
  171. ;** ModifyPages                                 **
  172. ;*************************************************
  173. ModifyPages:
  174.         rts
  175. ;ENDFOLD
  176. ;FOLD FreeMMUTable
  177. ;*************************************************
  178. ;** FreeMMUTable                                **
  179. ;** free the MMU table of the context *a0       **
  180. ;** the context MUST be unloaded                **
  181. ;** (has to be ensured by the caller)           **
  182. ;*************************************************
  183.         xdef FreeMMUTable
  184. FreeMMUTable:
  185.         saveregs a5
  186.  
  187.         move.l ctx_Root(a0),d0          ;a MMU table allocated ?
  188.         beq.s .exit
  189.  
  190.         move.l a0,a5
  191.         moveq #0,d1                     ;we're at level A
  192.         move.l d0,a0                    ;save the pointer
  193.  
  194.         bsr.s RecursiveRelease            ;release this stuff
  195. .exit:
  196.         loadregs
  197.         rts
  198. ;ENDFOLD
  199. ;FOLD RecursiveRelease
  200. ;*************************************************
  201. ;** RecursiveRelease                            **
  202. ;** Release the tree branch starting at *a0     **
  203. ;** at level d0, Context *a5                    **
  204. ;*************************************************
  205. RecursiveRelease:
  206.         saveregs d3-d4/a2-a3
  207.  
  208.         move.l d1,d4                    ;keep the level
  209.         move.l a0,a2
  210.         move.l a0,a3
  211.         cmp.w #4,d4                     ;are we at page level ?
  212.         beq.s .release                  ;if so, release immediately
  213.  
  214.         move.l mulib_LevelATableSz(a6,d4.w*4),d3        ;number of entries in this table
  215.         do
  216.          move.l (a2)+,d0                ;read pointer at this level
  217.          move.l d4,d1                   ;Level -> d1
  218.          jsr mulib_NextLevel(a6)        ;do we have a next level ?
  219.          tst.l d0
  220.          beq.s .nonext                  ;if not, then forget it
  221.          move.l d4,d1
  222.          move.l d0,a0                   ;the table pointer for the next level
  223.          addq.w #1,d1                   ;next level
  224.          tst.b mulib_LevelABits(a6,d1.w)        ;is this level available ?
  225.          bne.s .gotlevel
  226.          moveq #4,d1                    ;if not, we're at page level
  227. .gotlevel:
  228.          bsr.s RecursiveRelease         ;release the level below
  229. .nonext:
  230.          subq.l #1,d3
  231.         while.s ne
  232. .release:
  233.         move.l d4,d0                    ;at this level
  234.         move.l a3,a1                    ;this table
  235.                                         ;context in a5
  236.         bsr FreePointerTable            ;now release this level
  237.  
  238.         loadregs
  239.         rts
  240. ;ENDFOLD
  241.  
  242.  
  243.  
  244.  
  245.